package vn.conghuy.talkwithme;

import java.util.ArrayList;

import vn.conghuy.talkwithme.android.utils.Utils;
import vn.conghuy.talkwithme.data.android.ContactModel;
import vn.conghuy.talkwithme.responsor.action.FacebookAction;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.LocationManager;

public class MyApp extends Application {
	
	
	public ArrayList<ContactModel> listContact;
	public LocationManager locationManager;
	public float direction;
	public FacebookAction facebook;
	public Activity mainActivity;
	MySensorListener sensorListener;
	public SensorManager sensorMgr;
	boolean isEnableListenCompassChange;
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		isEnableListenCompassChange = false;
		
		listContact = new ArrayList<ContactModel>();
		Utils.ReadContactFromDbToList(this, listContact);
		
		locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		
		sensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE);
		sensorListener = new MySensorListener();
		sensorMgr.registerListener(sensorListener, sensorMgr.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
		
		facebook = FacebookAction.getInstance();
		facebook.init("105967222791491", this);
	}
	
	class MySensorListener implements SensorEventListener{
		
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}
		
		public void onSensorChanged(SensorEvent event) {
			direction = event.values[0];
		}
		
	}
	
	public void mainActivityResume(){
		listContact = new ArrayList<ContactModel>();
		Utils.ReadContactFromDbToList(this, listContact);
	}
	
	public void enableOrientationSensor(boolean isEnable){
		if(isEnable){
			sensorMgr.registerListener(sensorListener, sensorMgr.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
		}
		else{
			sensorMgr.unregisterListener(sensorListener);
		}
	}
}
